package com.imon.jmxbox;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class SpringFrontDeskImpl implements FrontDesk {

	private static final Log LOG = LogFactory.getLog(SpringFrontDeskImpl.class);

	@Resource
	private JmsTemplate jmsTemplate;

	@Value("${email}")
	private String email;
	
	public void sendMail(final Mail newMail) {
		jmsTemplate.send(new MessageCreator() {

			public Message createMessage(Session session) throws JMSException {
				LOG.debug("Email is set to: " + email);
				LOG.debug("Sending message using JmsTemplate");
				MapMessage message = session.createMapMessage();
				message.setString("mailId", newMail.getMailId());
				message.setString("country", newMail.getCountry());
				message.setDouble("weight", newMail.getWeight());
				return message;
			}
		});
	}
}
